package com.example.spmusic

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.example.spmusic.databinding.ActivityMainBinding
import com.example.spmusic.media.MusicPlayer
import com.example.spmusic.media.MusicViewModel

class MainActivity : AppCompatActivity() {
    private  lateinit var musicPlayer: MusicPlayer
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
        musicPlayer = MusicPlayer(assets)
        val viewModel = MusicViewModel(musicPlayer)
        val binding: ActivityMainBinding = //数据绑定写法
            DataBindingUtil.setContentView(this, R.layout.activity_main)

        // 加载音乐文件并传给 ViewModel
        val music = musicPlayer.musics.firstOrNull()
        viewModel.music = music

        // 把 ViewModel 绑定到布局（供 XML 使用）
        binding.musicViewModel = viewModel
        binding.playButton.setOnClickListener {
            viewModel.onButtonClicked()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        musicPlayer.release()
    }
}